package com.bread.tliaswebmanagement.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class JwtUtils {

    private static final String JWT_SECRET = "bread";
    private static final long JWT_EXPIRATION = 1000 * 3600; // 1小时

    public static String generateJwt(Map<String, Object> data){
        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, JWT_SECRET)
                .addClaims(data)
                .setExpiration(new Date(System.currentTimeMillis() + JWT_EXPIRATION))
                .compact();
    }

    public static Claims parseJwt(String jwt){
        return Jwts.parser()
                .setSigningKey(JWT_SECRET)
                .parseClaimsJws(jwt)
                .getBody();
    }

}
